﻿#include "canthread.h"
#include "ControlCAN.h"
#include <QTime>
#include <QCoreApplication>
#include <QMetaType>
#include <string.h>

#include <QMessageBox>

#include <QThread>
#include <QDebug>

CANThread::CANThread(QObject *parent) : QObject(parent)
{
    stopped = false;
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &CANThread::checkCANData);

}

CANThread::~CANThread()
{
    stop();
}



void CANThread::start()
{
    stopped = false;
    timer->start(30); // 每 30 毫秒检测一次
}





void CANThread::stop()
{
    stopped = true;
    timer->stop();
}



void CANThread::checkCANData()
{
    if (stopped)
        return;

    unsigned int dwRel;
    VCI_CAN_OBJ vco[200];

    dwRel = VCI_Receive(m_deviceType, m_debicIndex, 0, vco, 200, 0);
    if (dwRel > 0){
        emit getProtocolData(vco, dwRel, 0);
        VCI_ClearBuffer(m_deviceType,m_debicIndex,0);

    }

    dwRel = VCI_Receive(m_deviceType, m_debicIndex, 1, vco, 200, 0);
    if (dwRel > 0){
        emit getProtocolData(vco, dwRel, 1);
        VCI_ClearBuffer(m_deviceType,m_debicIndex,1);

    }
}

//1.打开设备
bool CANThread::openDevice(UINT deviceType,UINT debicIndex,UINT baundRate)
{
    m_deviceType = deviceType;/* USBCAN-2A或USBCAN-2C或CANalyst-II */
    m_debicIndex = debicIndex;/* 第1个设备 */
    m_baundRate = baundRate;
    unsigned int dwRel;
    bool state;
    dwRel = VCI_OpenDevice(m_deviceType, m_debicIndex, 0);
    if(dwRel != 1){
        state = false;
        qDebug()<<"open failed";
    }
    else{
        qDebug()<<"open success";
        state = true;
    }
    emit deviceOpened(state);
    return true;
}

//2.初始化CAN
bool CANThread::initCAN()
{
    unsigned int dwRel = VCI_ClearBuffer(m_deviceType, m_debicIndex, 0);
    dwRel = VCI_ClearBuffer(m_deviceType, m_debicIndex, 1);
    VCI_INIT_CONFIG vic;
    vic.AccCode=0x80000008;
    vic.AccMask=0xFFFFFFFF;
    vic.Filter=1;
    vic.Mode=0;
    switch (m_baundRate) {
    case 250:
        vic.Timing0=0x01;
        vic.Timing1=0x1c;
        break;
    case 500:
        vic.Timing0=0x00;
        vic.Timing1=0x1c;
        break;
    case 1000:
        vic.Timing0=0x00;
        vic.Timing1=0x14;
        break;
    default:
        break;
    }
    dwRel = VCI_InitCAN(m_deviceType, m_debicIndex, 0, &vic);
    dwRel = VCI_InitCAN(m_deviceType, m_debicIndex, 1, &vic);
    if(dwRel !=1)
        return false;
    else
        emit deviceInited();

    qDebug()<<"init success";

    return true;
}

//3.启动CAN
bool CANThread::startCAN()
{
    if(VCI_StartCAN(m_deviceType, m_debicIndex, 0) !=1)
    {
        qDebug()<<"start 0 fail.";
        return false;
    }
    else
        qDebug()<<"start 0 success.";
    if(VCI_StartCAN(m_deviceType, m_debicIndex, 1) !=1)
    {
        qDebug()<<"start 1 fail.";
        return false;
    }
    else
        qDebug()<<"start 1 success.";
    start();//启动计时器
    emit deviceStarted();
    return true;
}

//4.发送数据
bool CANThread::sendData(UINT channel,UINT ID,BYTE remoteFlag,BYTE externFlag,const unsigned char *data,BYTE len)
{
    qDebug()<<"开始发送";
    unsigned int dwRel;
    VCI_CAN_OBJ vco;
    vco.ID = ID ;
    vco.RemoteFlag = remoteFlag;
    vco.ExternFlag = externFlag;
    vco.DataLen = len;
    for(UINT j = 0;j < len;j++)
        vco.Data[j] = data[j];
    dwRel = VCI_Transmit(m_deviceType, m_debicIndex, channel,&vco,1);
    if(dwRel>0)
    {
        emit senddataend();
        qDebug()<<"发送成功";
        return true;
    }
    else
        return false;

}

//5.关闭设备
void CANThread::closeDevice()
{
    bool state =  VCI_CloseDevice(m_deviceType, m_debicIndex);
    stop();
    emit deviceClosed(state);
}

//0.复位设备，  复位后回到3
bool CANThread::reSetCAN()
{
    if(VCI_ResetCAN(m_deviceType, m_debicIndex, 0) !=1)
    {
        qDebug()<<"reset 0 fail.";
        return false;
    }
    else
        qDebug()<<"reset 0 success.";
    if(VCI_ResetCAN(m_deviceType, m_debicIndex, 1) !=1)
    {
        qDebug()<<"reset 1 fail.";
        return false;
    }
    else
        qDebug()<<"reset 1 success.";
    timer->stop();
    emit deviceReseted();
    return true;
}


